var jqAccordion = function(options){
	this.jqOpts = options;
	this.prevObj = null;
	this.prevIndex = -1;
	this.currentIndex = this.jqOpts.defaultIndex;
	this.objList = $(this.jqOpts.selector + this.jqOpts.header);
	this.custActiveClass = this.jqOpts.custActiveClass;
};

jqAccordion.prototype.getWidget = function(optHide){
	var opts = this.jqOpts;
	var selector = opts.selector;
	var fixDataPanel = opts.fixDataPanel;
	var initPanel = true;
	var setClass = this.setClass;
	var custActiveClass = this.custActiveClass;
	var headers = opts.header.split(">");
	var headerTag = "";
	if(headers.length > 0){
		headerTag = (headers[headers.length-1].replace(/ /g, "").length == 0) ? 
				"h3" : headers[headers.length-1];
	}
	
	$(selector).accordion({
		header: opts.header,
		icons: { "header": "ui-icon-circle-plus", "activeHeader": "ui-icon-circle-minus" },
		collapsible: opts.collapsible,
		active: (this.currentIndex >= 0) ? this.currentIndex : false, 
		heightStyle: "content", //fill | content | auto
		beforeActivate: function( event, ui ) {
			setClass(ui.oldHeader[0], custActiveClass, "");
			if($(">div", ui.oldPanel[0]).length > 0 && optHide) $(">div", ui.oldPanel[0]).hide();
		},
		activate: function (e, ui){
			var curIdx = $(selector).accordion("option", "active");
			var panelCnt = $(ui.oldPanel[0]).length;			
			if(fixDataPanel != null && fixDataPanel.length > 0){
				for(var i = 0; i < fixDataPanel.length; i++){					
					if(fixDataPanel[i] == curIdx){
						initPanel = false;						
						break;
					}
				}
			}			
			if(initPanel){
				if(panelCnt > 0) $(ui.oldPanel[0]).text("");
				$(ui.newPanel[0]).text("");
			}
			
			setClass(ui.newHeader[0], "ui-state-active", custActiveClass);
			if($(">div", ui.newPanel[0]).length > 0 && optHide) $(">div", ui.newPanel[0]).show();
			$(ui.oldHeader[0]).blur();
			$(ui.newHeader[0]).blur();
		}
//		activate: function (e, ui) {
//			$url = $(ui.newHeader[0]).children('span').text();
//			$.get($url, function (data) {
//				$(ui.newHeader[0]).next("div").css("height", "auto").html(data);
//			});
//		}
	}).sortable({
        axis: "y",
        handle: headerTag,//"h3",
        stop: function(event, ui) {
          // IE doesn't register the blur when sorting
          // so trigger focusout handlers to remove .ui-state-focus
          ui.item.children(headerTag).triggerHandler("focusout");
        }//$(ui.item).children()[0].tagName //"h3"
      });
	
	this.setPrevObj($(this.objList).eq(this.currentIndex));
	this.exeAction(this.prevObj);
};

jqAccordion.prototype.setClass = function(obj, oldClass, newClass, debug){
	var tmpClass = attrClass = $(obj).attr("class");
	if(attrClass == "undefined" || attrClass == null || attrClass.replace(/ /g, "").length == 0) return;
	
	tmpClass = attrClass.replace(new RegExp(oldClass,"g"), newClass);
	//if(debug) alert(tmpClass);	
	$(obj).removeClass();
	if(tmpClass != "undefined" && tmpClass.replace(/ /g, "").length > 0){
		var arrayClass = tmpClass.split(" ");
		for(var i = 0; i < arrayClass.length; i++){
			$(obj).addClass(arrayClass[i]);
		}
	}	
	//if(debug) alert($(obj).attr("class"));
};

jqAccordion.prototype.setPrevObj = function(obj){
	this.prevObj = obj;
};

jqAccordion.prototype.setPrevIndex = function(val){
	this.prevIndex = val;
};

jqAccordion.prototype.setCurrIndex = function(){
	this.currentIndex = $(this.jqOpts.selector).accordion("option", "active");
};

jqAccordion.prototype.detectUrl = function(obj){
	var url = $(obj).children("span").text();
	if(url.replace(/ /g, "") == "" || url == "undefined"){
		url = "";
	}	
	return url;
};

jqAccordion.prototype.exeAction = function(obj){
	this.setCurrIndex();
	
	//if(this.prevIndex == this.currentIndex) { return; };
	if(this.prevIndex == -1) this.setClass(obj, "ui-state-active", this.custActiveClass);
	
	var url = this.detectUrl($(obj));
	if(url != ""){ 
		loadAccordionContainer(url,"POST","html",$(obj), this);
	}else{
		var tmpObj = $(obj).next("div");
		if($(tmpObj).length > 0 && $(tmpObj).text().length > 0){
			this.setPrevObj($(obj));
			this.setPrevIndex(this.currentIndex);
		}
	}
	
	//alert(this.prevIndex +" || "+ this.currentIndex);
};

var loadAccordionContainer = function(path, mType, dtype, obj, widget) {
	var ajax = new jqMyAjax();
	var data = "";
	var option = new Array();
	option["type"] = mType;
	option["url"] = path;
	option["dataType"] = dtype;
	option["data"] = data;
	option["success"] = function(result){
		if(widget.prevIndex == -1) $(obj).next("div").css("height", "auto").html(result);
		else $(obj).next("div").html(result);
		
		widget.setPrevObj($(obj));
		widget.setPrevIndex(widget.currentIndex);
	};
	option["error"] = function(xhr, status, thrown){
		var extraMsg = "This tab selected is disabled.";
		var sessionOutRedirect = true;
		var codeDesc = ajax.errorCalback(xhr, extraMsg, sessionOutRedirect);
		if(codeDesc.indexOf("[0 ERROR]") != -1 || widget.prevIndex == -1) {
			$(widget.jqOpts.selector).accordion("option", "active", false);
			return;
		}
		
		$(obj).addClass( "ui-state-disabled" );
		$(widget.jqOpts.selector).accordion("option", "active", widget.prevIndex);
			
		widget.exeAction($(widget.prevObj));
	};

	ajax.callAjax(option);
};